<!--
 * @Description: 质检交车
 * @Autor: 何兴家
 * @Date: 2021-04-19 22:10:42
 * @LastEditors: 何兴家
 * @LastEditTime: 2021-05-21 14:47:13
-->
<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" :labelWidth="100" @filterChange="handleSearch" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="repair_repairReception_submitForSettlement_index"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :isSelectColumn="false"
      :datakey="'rows'"
      :onSyncTableData="handleSyncTableData"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>
    <BaseDialog :visible.sync="showPanel" title="审核交车" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <detail :rowData="checkedRowData" :pageType="editPageType" @close="showPanel = false" @refesh="handleRefeshTable" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { confirmAction, notifyAction, findFormItem, findTableItem } from '@/utils';
import dictCode from '@/utils/dictCode';

import detail from './detail';

import { getTestOrder, examTestOrder } from '@/api/repair/repairReception/submitForSettlement';
import { searchRoleByCode } from '@/api/common/empSelect/index';

export default {
  mixins: [dictionary],
  components: { detail },
  data() {
    return {
      topFilterList: this.createTopFIlterList(),
      columns: this.createTableColumns(),
      fetchapi: getTestOrder,
      fetchParams: { xhrAbort: false },
      showPanel: false,
      syncTableData: [], // 同步的table数据
      checkedRowData: {},
      editPageType: 'audit' // 详情页面类型 audit -- 审核 detail -- 预览
    };
  },
  mounted() {
    this.initDictData();
  },
  methods: {
    createTopFIlterList() {
      return [
        {
          type: 'SELECT',
          label: '交车状态',
          fieldName: 'deliveryTag',
          itemList: [
            { text: '待交车', value: 10041002 },
            { text: '已交车', value: 10041001 },
            { text: '全部', value: '' }
          ]
        },
        {
          type: 'SELECT',
          label: '预计交车时间',
          fieldName: 'endTimeSupposed',
          itemList: [
            { text: '当天', value: 'now' },
            { text: '全部', value: '' }
          ]
        },
        {
          type: 'SELECT',
          label: '服务顾问',
          fieldName: 'serviceAdvisor',
          itemList: []
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          render: props => {
            let btn =
              props.row.roStatus == 80491002 ? (
                <el-button
                  type="text"
                  onClick={() => {
                    this.handleCancelAudit(props.row);
                  }}
                >
                  取消
                </el-button>
              ) : (
                <el-button
                  type="text"
                  onClick={() => {
                    this.handleShowDetail(props.row, 'audit');
                  }}
                >
                  审核
                </el-button>
              );
            return (
              <div>
                {btn}
                <el-button
                  type="text"
                  onClick={() => {
                    this.handleShowDetail(props.row, 'detail');
                  }}
                >
                  预览
                </el-button>
              </div>
            );
          }
        },
        {
          title: '车牌',
          dataIndex: 'license',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '委托书号',
          dataIndex: 'roNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '服务顾问',
          dataIndex: 'serviceAdvisor',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '工单状态',
          dataIndex: 'roStatus',
          sorter: true,
          filter: true,
          filterType: 'input',
          dictItems: this.createDictList(8049)
        },
        {
          title: '是否交车',
          dataIndex: 'deliveryTag',
          sorter: true,
          filter: true,
          filterType: 'input',
          dictItems: this.createDictList(1004)
        },
        {
          title: '维修类型',
          dataIndex: 'repairTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户',
          dataIndex: 'ownerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户手机',
          dataIndex: 'mobile',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    collapseHandle() {
      this.nextTick(() => {
        this.refs.table.EXECUTE_RESET_HEIGHT();
      });
    },
    // 初始化数据查询
    initDictData() {
      this.queryServiceAdviserList();
    },
    // 获取服务顾问列表
    async queryServiceAdviserList() {
      let serviceConsultant = { role: dictCode.SERVICE_CONSULTANT };
      let res = await searchRoleByCode(serviceConsultant);
      if (res.resultCode == 200) {
        let serviceAdviserDict = res.data.map(item => ({ text: item.employeeName, value: item.userId + '' }));
        findFormItem(this.topFilterList, 'serviceAdvisor').itemList = serviceAdviserDict;
      }
    },
    // 点击搜索
    handleSearch(data) {
      this.fetchParams = { ...this.fetchParams, ...data };
    },
    // 刷新主页面数据
    handleRefeshTable() {
      this.fetchParams = { ...this.fetchParams };
    },
    // 点击取消审核
    async handleCancelAudit(row) {
      this.$refs.table.START_LOADING();
      try {
        let res = await examTestOrder({ deliveryTag: 10041002, roNo: row.roNo });
        this.$refs.table.STOP_LOADING();
        if (res.resultCode == 200) {
          notifyAction('取消审核成功', 'success');
          this.handleRefeshTable();
        }
      } catch (err) {
        this.$refs.table.STOP_LOADING();
        console.log(err, '取消审核失败');
      }
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    },
    // 点击审核或预览
    handleShowDetail(row, type) {
      this.checkedRowData = row;
      this.editPageType = type;
      this.showPanel = true;
    }
  }
};
</script>

<style scoped></style>
